package com.kexun.platform;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ModelAdapter {


    private JSONObject json;
    private int type;

    //音乐Json适配器
    public ModelAdapter(JSONObject json, int type) {
        this.json = json;
        this.type = type;
    }

    public MusicModel create() {
        MusicModel musicModel = new MusicModel();
        if (type == SOURCE.排行榜.getType()) {
            JSONObject data = json.getJSONObject("data");
            JSONArray singerJson = data.getJSONArray("singer");
            String singer = getSinger(singerJson);
            musicModel.setSinger(singer);
            String songmid = data.getString("songmid");
            musicModel.setSongmid(songmid);
            String albumname = data.getString("albumname");
            musicModel.setAlbumname(albumname);
            String songname = data.getString("songname");
            musicModel.setSongname(songname);
            Integer albumid = data.getInteger("albumid");
            musicModel.setAlbumid(albumid);
            Integer interval = data.getInteger("interval");
            musicModel.setInterval(interval);
        } else if (type == SOURCE.关键字.getType()) {
            JSONObject data = json;
            JSONArray singerJson = data.getJSONArray("singer");
            String singer = getSinger(singerJson);
            musicModel.setSinger(singer);
            String songmid = data.getString("songmid");
            musicModel.setSongmid(songmid);
            String albumname = data.getString("albumname");
            musicModel.setAlbumname(albumname);
            String songname = data.getString("songname");
            musicModel.setSongname(songname);
            Integer albumid = data.getInteger("albumid");
            musicModel.setAlbumid(albumid);
            Integer interval = data.getInteger("interval");
            musicModel.setInterval(interval);
        } else if (type == SOURCE.热门歌单.getType()) {
            JSONObject data = json;
            JSONArray singerJson = data.getJSONArray("singer");
            String singer = getSinger(singerJson);
            musicModel.setSinger(singer);
            String songmid = data.getString("mid");
            musicModel.setSongmid(songmid);
            String albumname = data.getJSONObject("album").getString("name");
            musicModel.setAlbumname(albumname);
            String songname = data.getString("name");
            musicModel.setSongname(songname);
            Integer albumid = data.getJSONObject("album").getInteger("id");
            musicModel.setAlbumid(albumid);
            Integer interval = data.getInteger("interval");
            musicModel.setInterval(interval);
        }
        return musicModel;
    }

    private String getSinger(JSONArray singers) {
        try {
            StringBuffer sgStr = new StringBuffer();
            for (int i = 0; i < singers.size(); i++) {
                JSONObject sg = (JSONObject) singers.get(i);
                String name = sg.getString("name");
                sgStr.append(name);
                if (i < singers.size() - 1) {
                    sgStr.append(",");
                }
            }
            return sgStr.toString();
        } catch (Exception e) {
            return "";
        }
    }

    class MusicModel {
        private String singer;
        private String songmid;
        private String albumname;
        private String songname;
        private Integer albumid;
        private Integer interval;

        public String getSinger() {
            return singer;
        }

        public void setSinger(String singer) {
            this.singer = singer;
        }

        public String getSongmid() {
            return songmid;
        }

        public void setSongmid(String songmid) {
            this.songmid = songmid;
        }

        public String getAlbumname() {
            return albumname;
        }

        public void setAlbumname(String albumname) {
            this.albumname = albumname;
        }

        public String getSongname() {
            return songname;
        }

        public void setSongname(String songname) {
            this.songname = songname;
        }

        public Integer getAlbumid() {
            return albumid;
        }

        public void setAlbumid(Integer albumid) {
            this.albumid = albumid;
        }

        public Integer getInterval() {
            return interval;
        }

        public void setInterval(Integer interval) {
            this.interval = interval;
        }
    }
}
